package cn.korilweb.demodictionary.service;

import cn.korilweb.demodictionary.dao.DictDao;
import cn.korilweb.demodictionary.entity.TreeNode;
import cn.korilweb.demodictionary.entity.bo.DictInfoBO;
import cn.korilweb.demodictionary.entity.co.DictParentChildCO;
import cn.korilweb.demodictionary.entity.dto.DictDTO;
import cn.korilweb.demodictionary.entity.dto.DictOrderDTO;
import cn.korilweb.demodictionary.entity.dto.DictStatusDTO;
import cn.korilweb.demodictionary.entity.dto.DictTypeDTO;
import cn.korilweb.demodictionary.entity.po.DictInfoPO;
import cn.korilweb.demodictionary.entity.po.DictTypePO;
import cn.korilweb.demodictionary.entity.vo.DictInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author DJH
 * @date 2021-10-26 21:28:33
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictDao dictDao;

    @Override
    public List<DictInfoVO> getTreeList() {
        List<DictParentChildCO> res = dictDao.getDictParentChildCOList();
        List<DictInfoBO> dictInfoBOList = dictDao.getDictInfoBOList();

        Map<Integer, TreeNode<DictInfoBO>> nodes = new HashMap<>();

        dictInfoBOList.forEach(row -> {
            TreeNode<DictInfoBO> node = new TreeNode<>();
            node.setData(row);
            nodes.put(row.getInfoId(), node);
        });

        res.forEach(row -> {
            Integer id = row.getInfoId();
            TreeNode<DictInfoBO> node = nodes.get(id);
            node.setParent(nodes.get(row.getParentInfoId()));
            node.setParentInfoId(row.getParentInfoId());
            if (node.getChildren() == null) {
                node.setChildren(new ArrayList<>());
            }
            node.getChildren().add(nodes.get(row.getChildInfoId()));
        });
//        List<TreeNode<DictInfoBO>> dictInfoVO = new ArrayList<>();
        Map<String, DictInfoVO> dictTypeIdInfoVOMap = new HashMap<>();
        for (Map.Entry<Integer, TreeNode<DictInfoBO>> entry : nodes.entrySet()) {
            if (entry.getValue().getParent() == null) {
                int typeId = entry.getValue().getData().getTypeId();
                DictTypePO dictType = dictDao.getDictTypeById(typeId);
                DictInfoVO vo = new DictInfoVO();
                vo.setTypeName(dictType.getTypeName());
                vo.setTypeCode(dictType.getTypeCode());
                vo.setTypeId(dictType.getId());
                if (vo.getDict() == null) {
                    vo.setDict(new ArrayList<>());
                }
                vo.getDict().add(entry.getValue());
                if (dictTypeIdInfoVOMap.get(dictType.getTypeCode()) != null) {
                    dictTypeIdInfoVOMap.get(dictType.getTypeCode()).getDict().add(entry.getValue());
                }
                else {
                    dictTypeIdInfoVOMap.put(dictType.getTypeCode(), vo);
                }
            }
        }
        List<DictInfoVO> dictInfoVO = new ArrayList<>();
        for (Map.Entry<String, DictInfoVO> entry : dictTypeIdInfoVOMap.entrySet()) {
            dictInfoVO.add(entry.getValue());
        }
        return dictInfoVO;
    }

    @Override
    public void addDict(DictDTO dictDTO) {
        DictInfoPO info = new DictInfoPO();
        BeanUtils.copyProperties(dictDTO, info);
        info.setEnabled(1);
        info.setLevel(1000);
        dictDao.addDictInfo(info);
    }

    @Override
    public void modifyDict(Integer infoId, DictDTO dictDTO) {
        dictDao.updateDict(infoId, dictDTO);
    }

    @Override
    public void changeOrder(DictOrderDTO dictOrderDTO) {
        int infoId = dictOrderDTO.getInfoId();
        String orderCmd = dictOrderDTO.getOrder();
        // 同级节点的总数
        int count = dictDao.getSameLevelCnt(infoId);
        // 当前节点的顺序
        int orderNo = dictDao.getOrderNoById(infoId);

        // 当前节点的父节点 id
        int parentInfoId = dictDao.getParentInfoId(infoId);

        if (orderNo == 1 && "up".equals(orderCmd)) {
            System.out.println("排第一个，无法继续上调顺序");
            return;
        }
        else if (orderNo == count && "down".equals(orderCmd)) {
            System.out.println("排最后一个，无法继续下调顺序");
            return;
        }
        if ("up".equals(orderCmd)) {
            // 获取前一个节点
            DictInfoBO preInfo = dictDao.getInfo(parentInfoId, orderNo - 1);
            dictDao.updateOrder(infoId, orderNo - 1);
            dictDao.updateOrder(preInfo.getInfoId(), orderNo);
        }
        else if ("down".equals(orderCmd)) {
            // 获取后一个节点
            DictInfoBO nxtInfo = dictDao.getInfo(parentInfoId, orderNo + 1);
            dictDao.updateOrder(infoId, orderNo + 1);
            dictDao.updateOrder(nxtInfo.getInfoId(), orderNo);
        }
    }

    @Override
    public void addType(DictTypeDTO dictTypeDTO) {
        DictTypePO dictTypePO = new DictTypePO();
        BeanUtils.copyProperties(dictTypeDTO, dictTypePO);
        dictDao.addDictType(dictTypePO);
    }

    @Override
    public void changeStatus(DictStatusDTO dictStatusDTO) {
        Integer infoId = dictStatusDTO.getInfoId();
        String status = dictStatusDTO.getStatus();
        Integer statusValue = 0;
        if ("enable".equals(status)) {
            statusValue = 1;
        }
        else if ("disable".equals(status)) {
            statusValue = 0;
        }
        dictDao.updateStatus(infoId, statusValue);
    }
}
